#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef struct 
{
    int *nums;
    int front;
    int rear; 
    int capacity;
}ArrayQueue;

/*构造函数*/
ArrayQueue *newArrayQueue(int capacity)
{
    ArrayQueue *q = (ArrayQueue *)malloc(sizeof(ArrayQueue));
    q->nums = (int *)malloc(capacity * sizeof(int));
    q->front = q->rear = 0;
    q->capacity = capacity;
    return q;
}
/*析构函数*/
void delArrayQueue(ArrayQueue *q)
{
    free(q->nums);
    free(q);
}
/*获取capacity*/
int capacity(ArrayQueue *q)
{
    return q->capacity;
}
/*获取size*/
int size(ArrayQueue *q)
{
    return q->rear - q->front;
}
/*是否为空*/
bool isEmpty(ArrayQueue *q)
{
    return q->front == q->rear;
}
/*入队*/
void push(ArrayQueue *q, int val)
{
    if (size(q) == q->capacity)
    {
        printf("队列已满\n");
        return;
    }
    q->nums[(q->rear) % q->capacity] = val;
    q->rear++;    
}
/*输出队首*/
int peek(ArrayQueue *q)
{
    assert(size(q));
    return q->nums[(q->front)%q->capacity];    
}
/*出队*/
int pop(ArrayQueue *q)
{
    int val = peek(q);
    q->front++;
    return val; 
}
/*打印队列*/
void printQueue(ArrayQueue *q)
{
    if (size(q)==0)
    {
        printf("队列为空\n");
        return;
    }
    for (int i = q->front; i < q->rear; i++)
    {
        printf("%d ", q->nums[i%q->capacity]);
    }
    printf("\n");
}

int main(int argc, char const *argv[])
{
    // 初始化，头1-3-2-5-4
    ArrayQueue *q = newArrayQueue(3);
    push(q, 3);
    push(q, 1);
    push(q, 2);
    // 队列信息1
    printf("front:%d,rear:%d\n", q->front, q->rear);
    printQueue(q);
    
    // 出队
    pop(q);
    printf("出队:");
    printQueue(q);
    // 入队
    printf("入队\n");
    push(q, 99);
    // 队列信息2
    printf("front:%d,rear:%d\n", q->front, q->rear);
    printQueue(q);

    // 析构
    delArrayQueue(q);
    return 0;
}
